package com.tz.farm_feeding.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.tz.farm_feeding.domain.Pig;
import com.tz.farm_feeding.domain.PigExample;
import com.tz.farm_feeding.domain.Sell;
import com.tz.farm_feeding.domain.SellExample;
import com.tz.farm_feeding.domain.status;
import com.tz.farm_feeding.mapper.PigMapper;
import com.tz.farm_feeding.mapper.SellMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@RestController
@RequestMapping("/sell")
public class SellController {

  @Autowired
  SellMapper sellMapper;

  @PostMapping(value = "/getAllSellInfo")
  public String getAllSellInfo(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {

    JSONObject jsonObject = new JSONObject();
    List<Sell> sells = sellMapper.selectByExample(new SellExample());
    JSONArray jsonArray = new JSONArray();
    for (Sell sell : sells) {
      jsonArray.add(sell);
    }
    jsonObject.put("result", status.s_ok);
    jsonObject.put("data", jsonArray);
    return jsonObject.toJSONString();
  }

  @PostMapping(value = "/addSellInfo")
  public String addSellInfo(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {

    JSONObject jsonObject = new JSONObject();
    Sell sell = new Sell();

    sell.setDate(jsonparm.getDate("date"));
    sell.setSelldate(jsonparm.getDate("selldate"));

    sell.setPrice(jsonparm.getDouble("price"));
    sell.setMarketprice(jsonparm.getDouble("marketprice"));
    sell.setSellcount(jsonparm.getInteger("sellcount"));
    sellMapper.insert(sell);
    jsonObject.put("result", status.s_ok);
    return jsonObject.toJSONString();
  }

  @PostMapping(value = "/delSell")
  public String delSell(@RequestBody JSONObject jsonparm, HttpServletRequest request, HttpSession session) {
    JSONObject resp = new JSONObject();
    int id = jsonparm.getInteger("id");
    sellMapper.deleteByPrimaryKey(id);
    resp.put("result", status.s_ok);
    return resp.toJSONString();
  }

}